﻿package component.fileupload.controller;

import java.io.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;

import component.fileupload.action.FileUploadActionFactory;
import component.fileupload.action.IFileUploadAction;
import component.fileupload.exception.FileUploadException;

public class UploadServlet extends HttpServlet {
	private IFileUploadAction uploadAction = FileUploadActionFactory
			.getFileUploadAction();

	private ServletConfig servletConfig;

	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return servletConfig;
	}

	public String getServletInfo() {
		// TODO Auto-generated method stub
		return servletConfig.getServletName();
	}

	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		this.servletConfig = config;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		String isReturn = request.getParameter("isReturn");
		String action = request.getParameter("action");
		if ("getState".equals(action)) {
			uploadAction.getState(request.getSession(), response);
		} else if ("receiveFile".equals(action)) {
			String fileName = request.getParameter("saveFileName");
			if(fileName == null) fileName = "上传的文件";
			String ret = null;
			try {
				ret = uploadAction.receiveFile(request, response, fileName);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			String url = "/common/fileupload.jsp";

			String uploadRet = "";
			String operate = request.getParameter("operate");
			try {
				if(operate != null) {
					uploadRet = uploadAction.doUploadToOther(servletConfig, request, response);
				} else {
					uploadRet = uploadAction.doUpload(servletConfig, request, response);
				}
				uploadAction.destroy();
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String fieldName = request.getParameter("fieldName");
			/*
			String retPage = request.getParameter("retPage");
			if(operate != null && !"1".equals(uploadRet)) {//返回原页面提示错误信息
				request.setAttribute("uploadRet", uploadRet);
				request.setAttribute("fieldName", request.getParameter("fieldName"));

				// RequestDispatcher reqDispatcher = request.getRequestDispatcher(retPage);
				// reqDispatcher.forward(request, response);
				response.sendRedirect(request.getContextPath() + retPage + "?uploadRet=" + uploadRet );// 需要加上下文路径

			} else {*/
				//防止中文乱码
				response.setContentType("text/html");
				response.setCharacterEncoding("UTF-8");
				uploadRet = java.net.URLEncoder.encode(uploadRet, "UTF-8");

				String str = "<script>";
				if(operate == null) {
					str += "window.opener.document.getElementById('" + fieldName + "').value = '" + uploadRet + "';";
				} else {
					if("1".equals(uploadRet)) {
						str += "try{window.opener.doAfterUpload();}catch(e){}" ;
					} else {
						str += "alert(decodeURI('" + uploadRet + "'));" ;
					}
				}
				
				str += "window.close();";
				str += "</script>";
				ServletOutputStream out = response.getOutputStream();
				out.write(str.getBytes());
				out.flush();
				out.close();
				out = null;
			}
		//}

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doPost(request, response);
	}

}
